//
//  YRJCityGoFreedomTableViewCell.m
//  YanRenjie
//
//  Created by dllo on 15/10/30.
//  Copyright (c) 2015年 yanrenjie. All rights reserved.
//

#import "YRJCityGoFreedomTableViewCell.h"
#import "YRJTripHeader.h"
@implementation YRJCityGoFreedomTableViewCell
- (void)dealloc {
    [_myflowLayout release];
    [_mycollectionView release];
    [_newDiscount release];
    
    [super dealloc];
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createCell];
    }
    return self;
}

- (void)createCell {
    self.myflowLayout = [[UICollectionViewFlowLayout alloc] init];
    self.myflowLayout.itemSize = CGSizeMake((180.0 / 375) * SWIDTH, (260.0 / 667) * SHEIGHT);
    self.myflowLayout.minimumInteritemSpacing = (5.0 / 375) * SWIDTH;
    self.myflowLayout.minimumLineSpacing = 0;
    self.myflowLayout.sectionInset = UIEdgeInsetsMake((10.0 / 667) * SHEIGHT, (5.0 / 375) * SWIDTH, (10.0 / 667) * SHEIGHT, (5.0 / 375) * SWIDTH);
    
    self.mycollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, SWIDTH, (260.0 / 667) * SHEIGHT  * 3) collectionViewLayout:self.myflowLayout];
    [_mycollectionView release];
    self.mycollectionView.backgroundColor = [UIColor whiteColor];
    [self.mycollectionView registerClass:[YRJCityGoFreedomCollectionViewCell class] forCellWithReuseIdentifier:@"cityCollectionCellReuse"];
    self.mycollectionView.dataSource = self;
    self.mycollectionView.delegate = self;
    self.mycollectionView.scrollEnabled = NO;
    [self.contentView addSubview:self.mycollectionView];
    [_myflowLayout release];
}

#pragma mark - collectionView两个必须实现的协议方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.newDiscount.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    YRJCityGoFreedomCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cityCollectionCellReuse" forIndexPath:indexPath];
    if (self.newDiscount.count != 0) {
        [cell.photoImageView sd_setImageWithURL:[NSURL URLWithString:[self.newDiscount[indexPath.row] photo]] placeholderImage:[UIImage imageNamed:@"placeHold.png"]];
        cell.titleLabel.text = [self.newDiscount[indexPath.row] title];
        cell.priceLabel.text = [[[self.newDiscount[indexPath.row] price] stringByReplacingOccurrencesOfString:@"<em>" withString:@""] stringByReplacingOccurrencesOfString:@"</em>" withString:@""];
        cell.priceoffLabel.text = [self.newDiscount[indexPath.row] priceoff];
    }
    return cell;
}

#pragma mark - collectionView 的点击方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    [self.delegate pushToCityGoFreedomWebView:(NSString *)[self.newDiscount[indexPath.row] id]];
}


#pragma mark - 从tableView中传过来的数组的刷新方法, 重点内容(如果在tableView的cell中利用点语法刷新, 使得程序的耦合性加大, 在此刷新可以做到低耦合)
- (void)setNewDiscount:(NSArray *)newDiscount {
    if (_newDiscount != newDiscount) {
        _newDiscount = newDiscount;
    }
    [self.mycollectionView reloadData];
}

@end
